package com.mall.app.web;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.app.bean.TbBuyer;
import com.mall.app.bean.TbGoods;
import com.mall.app.bean.TbLogistic;
import com.mall.app.bean.TbOrder;
import com.mall.app.bean.TbUser;
import com.mall.app.service.TbGoodsService;
import com.mall.app.service.TbOrderService;
import com.mall.app.utils.enumeration.OrderStatus;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author LeeSin
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/order")
public class TbOrderController {
	@Autowired
	private TbOrderService orderService;
	@Autowired
	private TbGoodsService goodsService;
	/**
	 * 后端类别分页方法
	 * @return
	 */
	
	@RequestMapping("/listPage")
	public Map<String,Object> listPage(int page,int limit,Integer oid,Integer stat,Integer buyerId){
		
		Map<String,Object>  map=new HashMap<String, Object>();
		map.put("page", (page-1)*limit);
		map.put("size", limit);
		map.put("oid", oid);//查询使用订单编号作条件
		map.put("buyerId", buyerId);
		map.put("stat", stat);
		Map<String,Object>  result=new HashMap<String, Object>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("count", orderService.getCount(map));
		result.put("data", orderService.listPage(map));
		return result;
	}
	
	
	
	@RequestMapping("/delete")
	public boolean removeOrder(@RequestParam("oids[]")List<Integer> oids){
		return orderService.removeByIds(oids);
	}
	
	@RequestMapping("/updateLogistic")
	public boolean updateOrderLogistic(TbOrder order,TbGoods goods,Integer gid,Integer lid,Integer bid,Integer uid){
		LocalDateTime now = LocalDateTime.now();
		order.setModifyTime(now);
		if(gid!=null)
		{
			TbGoods good=new TbGoods();
			good.setGid(gid);
			order.setGoods(good);
		}
		if(bid!=null)
		{
			TbBuyer buyer=new TbBuyer();
			buyer.setBid(bid);
			order.setBuyer(buyer);
		}
		if(lid!=null)
		{
			TbLogistic logistic=new TbLogistic();
			logistic.setLid(lid);
			order.setLogistic(logistic);
		}
		if(uid!=null)
		{
			TbUser user=new TbUser();
			user.setUid(uid);
			order.setUser(user);
		}
		boolean a=goodsService.updateGoodsQuantity(goods);
		boolean b=orderService.updateOrderLogistic(order);
		if(a&&b) return true;
		else return false;
	}
	
	@RequestMapping("/updateStat")
	public boolean updateOrderStat(TbOrder order){
		LocalDateTime now = LocalDateTime.now();
		order.setModifyTime(now);
		return orderService.updateOrderLogistic(order);
	}
	@RequestMapping("/update")
	public boolean updateOrder(TbOrder order,Integer bid,Integer gid,Integer lid,Integer uid) {
		LocalDateTime now = LocalDateTime.now();
		order.setModifyTime(now);
		if(gid!=null)
		{
			TbGoods goods=new TbGoods();
			goods.setGid(gid);
			order.setGoods(goods);
		}
		if(bid!=null)
		{
			TbBuyer buyer=new TbBuyer();
			buyer.setBid(bid);
			order.setBuyer(buyer);
		}
		if(lid!=null)
		{
			TbLogistic logistic=new TbLogistic();
			logistic.setLid(lid);
			order.setLogistic(logistic);
		}
		if(uid!=null)
		{
			TbUser user=new TbUser();
			user.setUid(uid);
			order.setUser(user);
		}
		return orderService.updateOrder(order);
	}
	
	@RequestMapping("/insert")
	public boolean insertOrder(TbOrder order,Integer bid,Integer gid,Integer lid,Integer uid) {
		LocalDateTime now = LocalDateTime.now();
		order.setModifyTime(now);
		order.setCreateTime(now);
		if(gid!=null)
		{
			TbGoods goods=new TbGoods();
			goods.setGid(gid);
			order.setGoods(goods);
		}
		if(bid!=null)
		{
			TbBuyer buyer=new TbBuyer();
			buyer.setBid(bid);
			order.setBuyer(buyer);
		}
		if(lid!=null)
		{
			TbLogistic logistic=new TbLogistic();
			logistic.setLid(lid);
			order.setLogistic(logistic);
		}
		if(uid!=null)
		{
			TbUser user=new TbUser();
			user.setUid(uid);
			order.setUser(user);
		}
		return orderService.insertOrder(order);
	}
	
	@RequestMapping("/purchaseGoods")
	public boolean purchaseGoods(int bid,int gid,int goodsQuantity) {
		LocalDateTime now = LocalDateTime.now();
		TbOrder order = new TbOrder();
		//设置修改时间和创建时间
		order.setModifyTime(now);
		order.setCreateTime(now);
		
		TbGoods goods=new TbGoods();
		goods.setGid(gid);
		order.setGoods(goods);
		
		
		TbBuyer buyer=new TbBuyer();
		buyer.setBid(bid);
		order.setBuyer(buyer);
		
		order.setGoodsQuantity(goodsQuantity);
		
		order.setStat(OrderStatus.CREATED.getIndex());//设置订单状态
		
		return orderService.insertOrder(order);
	}
}

